<?php
/**
 * Created by PhpStorm.
 * User: ASUS
 * Date: 2021-03-08
 * Time: 9:13
 */

namespace App\Traits;

use App\Extensions\Log\V1\InstanceLog;
use Illuminate\Support\Facades\Response;
use Symfony\Component\HttpFoundation\Response as FoundationResponse;

trait ApiResponse
{
    /**
     * @var int
     */
    protected $httpStatusCode = FoundationResponse::HTTP_OK;

    /**
     * @return int
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/18/17:10
     */
    private function getHttpStatusCode()
    {
        return $this->httpStatusCode;
    }

    /**
     * @param int $httpCode
     * @return $this
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/18/17:10
     */
    public function setHttpStatusCode($httpCode = 200)
    {
        $this->httpStatusCode = $httpCode;

        return $this;
    }

    /**
     * @param $data
     * @param array $header
     * @return \Illuminate\Http\JsonResponse
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/18/17:10
     */
    public function respond($data, $header = [])
    {
        // 打印日志
        $request = request();
        $debugId = uniqid();
        if (config('app.debug')) {
            $data['debug_id'] = $debugId;
        }
        $debugData = [
            'LOG_ID'         => $debugId,
            'IP_ADDRESS'     => $request->ip(),
            'REQUEST_URL'    => $request->fullUrl(),
            'REQUEST_METHOD' => $request->method(),
            'HEADERS'        => $request->header(),
            'PARAMETERS'     => $request->all(),
            'RESPONSES'      => $data,
        ];
        InstanceLog::instance('request')->debug($debugId, $debugData);

        return Response::json($data, $this->getHttpStatusCode(), $header);
    }

    /**
     * @param $data
     * @param null $httpCode
     * @param array $header
     * @return \Illuminate\Http\JsonResponse
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/18/17:10
     */
    public function status($data, $httpCode = null, $header = [])
    {
        if ($httpCode) {
            $this->setHttpStatusCode($httpCode);
        }

        return $this->respond($data, $header);
    }

    /**
     * @param null $data
     * @return \Illuminate\Http\JsonResponse
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/18/17:10
     */
    public function success($data = null)
    {
        $data = array_merge(body_format(0), ['result' => $data]);

        return $this->status($data);
    }

    /**
     * @param int $code
     * @param string $desc
     * @param array $extra
     * @param array $header
     * @param int $httpCode
     * @return \Illuminate\Http\JsonResponse
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/18/17:06
     */
    public function failed($code = 500, $desc = '', $extra = [], $header = [], $httpCode = FoundationResponse::HTTP_OK)
    {
        $desc = $desc ? : trans('exception/v1/general.' . $code);
        $data = empty($extra) ? body_format($code, $desc) : array_merge(body_format($code, $desc), $extra);

        return $this->status($data, $httpCode, $header);
    }
}
